<!DOCTYPE html>
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<script src="/resources/test-only-api.js"></script>
<script src="/webusb/resources/fake-devices.js"></script>
<script src="/webusb/resources/usb-helpers.js"></script>
<body>
<script>
'use strict';

usb_test(() => {
  return navigator.usb.test.initialize().then(() => {
    return new Promise(async (resolve, reject) => {
      let iframe = document.createElement('iframe');
      iframe.src = '/webusb/resources/open-in-iframe.html';
      document.body.appendChild(iframe);
      await navigator.usb.test.attachToContext(iframe);

      window.onmessage = messageEvent => {
        if (messageEvent.data == 'Ready') {
          document.body.removeChild(iframe);
          GCController.collect();
          navigator.usb.test.addFakeDevice(fakeDeviceInit);
          resolve();
        } else {
          reject(messageEvent.data);
        }
      };

      iframe.contentWindow.postMessage('Ready', '*');
    });
  });
}, 'events are not delivered to detached frames');
</script>
</body>
